<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Product extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://localhost/ci/index.php/product
	 *	- or -  
	 * 		http://localhost/ci/index.php/product/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://localhost/booms10/
	 *
	 * 
	 */
    
    /*  
     * Name: index
     * Author: Gafs
     * Date: 16/06/2011
     * 
     * 
     * @param   $offset
     * @return  data object
     */

    public function __construct() {
        
        parent::__construct();
        
        $this->load->model('product_model');
        
        // category group
        $this->categoryGroups = array(
            'male'=>'Nam',
            'female' =>'Nữ',
            'accessory' =>'Phụ kiện'
        );
        
        // size array
        $this->data['arrSizeChar'] = array('XXS','XS','S','M','L','XL','XXL');
        $this->data['arrSizeNumber'] = array();
        $i = 28;
        for ($i=28;$i<=44;$i++) {
            $this->data['arrSizeNumber'][$i] = $i;
        }
        
        // categories
        foreach($this->categoryGroups as $catCode => $catName) {
            $codeName = 'categories'.$catCode;
            $$codeName = $this->product_model->get_categories_by_type($catCode);
            $this->data[$codeName] = $$codeName;
        }
        $this->categories = $this->product_model->get_all_categories();
        foreach ($this->categories as $category){
            $this->categoriesArray[$category->pcat_code] = $category->pcat_name;
        }
        
        // brands
        $this->brands = $this->product_model->get_brands();
        foreach ($this->brands as $brand){
            $this->brandsArray[$brand->brand_alias] = $brand->brand_name;
        }
        
    }
    
    
    
	public function index($offset = 0)
	{
        $data = $this->data;
        // limit item per page
		$limit = 20;
        
        // default filter values
        $data['current_brandcode'] = 'all';
        $data['current_material'] = 'all';
        $data['current_pcat_code'] = 'all';
        
        // getting filter data
        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            
            // reset offsett value for index method
            if($this->router->method == 'index') {
                $offset = 0;
            }
            $action = $this->input->post('action');
            if($action == 'showAll' OR $action == 'btnSubmit') {
                $this->input->set_cookie('strWhere',null,0);
                $this->input->set_cookie('strWhere',null,0);
                $strWhere   = $this->input->cookie('strWhere');
                $arrWhere   = unserialize($strWhere);
                $strLike    = $this->input->cookie('strLike');
                $arrLike    = unserialize($strLike);
            } else {
                // where condition
                $categoryCode   = $this->input->post('filterCategory');
                $brandCode      = $this->input->post('filterBrand');
                $materialText   = $this->input->post('filterMaterial');
                $searchID       = $this->input->post('searchID');
                $arrWhere       = array();

                if ($categoryCode !== '' && $categoryCode != 'all'){
                    $arrWhere['pcat_code'] = $categoryCode;
                    $data['current_pcat_code'] = $categoryCode;
                }
                if ($brandCode !== '' && $brandCode != 'all'){
                    $arrWhere['brandcode'] = $brandCode;
                    $data['current_brandcode'] = $brandCode;
                }
                if ($materialText !== '' && $materialText != 'all'){
                    $arrWhere['material'] = $materialText;
                    $data['current_material'] = $materialText;
                }
                if ($searchID !== ''){
                    $arrWhere['p_id'] = $searchID;
                    $data['current_p_id'] = $searchID;
                }
                $this->input->set_cookie('strWhere',serialize($arrWhere),0);

                // like condition
                $searchName = $this->input->post('searchName');
                $arrLike = array();
                if ($searchName !== ''){
                    $arrLike['p_name'] = $searchName;
                    $data['current_p_name'] = $searchName;
                }


                $this->input->set_cookie('strLike',serialize($arrLike),0);
            }
        } else {
            $strWhere = $this->input->cookie('strWhere');
            $arrWhere = unserialize($strWhere);
            $strLike = $this->input->cookie('strLike');
            $arrLike = unserialize($strLike);
            
        } // end of getting filter data
        
        $data['offset'] = $offset; // track current offset value
        
        //var_dump($arrWhere);
		if($arrWhere == true) {
        //    $result = $this->product_model->get_all($limit, $offset);
        //} else {
            foreach($arrWhere as $field => $fieldValue){
                $data["current_$field"] = $fieldValue;
            }
        }
        
        if ($arrLike == true){
            foreach($arrLike as $field => $fieldValue){
                $data["current_$field"] = $fieldValue;
            }
        }
        
            $result = $this->product_model->get_by_filter($arrWhere, $arrLike, $limit, $offset);
        //
		
		$data['products'] = $result['rows'];
		$data['totalRows'] = $result['totalRows'];
        
        // categories
        $data['categoryGroups'] = $this->categoryGroups;
        $data['categories'] = $this->categories;
        $data['categoriesArray'] = $this->categoriesArray;
        
        // brands
        $data['brands'] = $this->brands;
        $data['brandsArray'] = $this->brandsArray;
        
        // material
        $materials = $this->product_model->get_materials();
        $data['materials'] = $materials;
                
		// pagination
		$this->load->library('pagination');
		$config = array();
		$config['base_url'] = site_url()."/product/index/";
		$config['total_rows'] = $data['totalRows'];
		$config['per_page'] = $limit;
		$config['uri_segment'] = 3;
        $config['num_links'] = 3;

		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		
		$this->load->view('home', $data);
	}
        
    public function update_status($offset = 0) {
        
        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $dataUpdate = $this->input->post('data');
            $checkbox = $this->input->post('selectedBox');
            list($field,$status,$id) = explode('-',$dataUpdate);
            $data = array(
                $field => $status
            );
            $this->product_model->update_by_id($id,$data);
            $this->index($offset);
        }
    }
    
    public function update($field, $id) {
        $data = $this->data;
        $result = $this->product_model->get_by_id($id);
        $data['productInfo'] = $result;
        // categories
        $data['categoryGroups'] = $this->categoryGroups;
        $data['categories'] = $this->categories;
        $data['categoriesArray'] = $this->categoriesArray;
        $data['current_pcat_code'] = $result->pcat_code;
        // brands
        $data['brands'] = $this->brands;
        $data['brandsArray'] = $this->brandsArray;
        $data['current_brandcode'] = $result->brandcode;
        
        
        $this->load->view('product/update',$data);
        
        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $dataUpdate['p_name']     = $this->input->post('pName');
            $dataUpdate['p_code']     = $this->input->post('pCode');
            $dataUpdate['p_date']     = $this->input->post('pDate');
            $dataUpdate['pcat_code']  = $this->input->post('pCatCode');
            $dataUpdate['brandcode']  = $this->input->post('pBrand');
            $dataUpdate['p_des']      = $this->input->post('pDes');
            $pictures                 = $this->input->post('pPictures'); 
            $dataUpdate['p_pictures'] = str_replace(" ","",$pictures); // remove blank(s)
            $dataUpdate['color']      = $this->input->post('pColor');
            $dataUpdate['color_img']  = $this->input->post('pColorImg');
            $dataUpdate['material']   = $this->input->post('pMaterial');
            $size                     = $this->input->post('pSize');
            $dataUpdate['size']       = implode(",",$size);
            $price                    = $this->input->post('pPrice');
            $dataUpdate['price']      = str_replace(" ","",$price); // change back to normal number format
            $dataUpdate['status']     = $this->input->post('pStatus');
            $dataUpdate['is_hot']     = $this->input->post('pIsHot');
            $dataUpdate['is_home']    = $this->input->post('pIsHome');
            $dataUpdate['is_sale']    = $this->input->post('pIsSale');
            $dataUpdate['sale_type']  = $this->input->post('pSaleType');
            $dataUpdate['sale_value'] = $this->input->post('pSaleValue');
            $dataUpdate['ordering']   = $this->input->post('pOrdering');
            
            $action = $this->input->post('action');
            $this->product_model->update_by_id($id,$dataUpdate);
            if($action == 'btnSave') {
                redirect("product/update/$id");
            } else {
                redirect('/product/');
            }
            
            
        }
        
    }
    
    public function add() {
        $data = $this->data;
        
        // categories
        $data['categoryGroups'] = $this->categoryGroups;
        $data['categories'] = $this->categories;
        $data['categoriesArray'] = $this->categoriesArray;
        // brands
        $data['brands'] = $this->brands;
        $data['brandsArray'] = $this->brandsArray;
        
        $this->load->view('product/add',$data);
        
        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $dataUpdate['p_name']     = $this->input->post('pName');
            $dataUpdate['p_code']     = $this->input->post('pCode');
            $dataUpdate['p_date']     = $this->input->post('pDate');
            $dataUpdate['pcat_code']  = $this->input->post('pCatCode');
            $dataUpdate['brandcode']  = $this->input->post('pBrand');
            $dataUpdate['p_des']      = $this->input->post('pDes');
            $pictures                 = $this->input->post('pPictures'); 
            $dataUpdate['p_pictures'] = str_replace(" ","",$pictures); // remove blank(s)
            $dataUpdate['color']      = $this->input->post('pColor');
            $dataUpdate['color_img']  = $this->input->post('pColorImg');
            $dataUpdate['material']   = $this->input->post('pMaterial');
            $size                     = $this->input->post('pSize');
            $dataUpdate['size']       = implode(",",$size);
            $price                    = $this->input->post('pPrice');
            $dataUpdate['price']      = str_replace(" ","",$price); // change back to normal number format
            $dataUpdate['status']     = $this->input->post('pStatus');
            $dataUpdate['is_hot']     = $this->input->post('pIsHot');
            $dataUpdate['is_home']    = $this->input->post('pIsHome');
            $dataUpdate['is_sale']    = $this->input->post('pIsSale');
            $dataUpdate['sale_type']  = $this->input->post('pSaleType');
            $dataUpdate['sale_value'] = $this->input->post('pSaleValue');
            $dataUpdate['ordering']   = $this->input->post('pOrdering');
            
            $action = $this->input->post('action');
            $this->product_model->add($dataUpdate);
            if($action == 'btnSave') {
                $this->load->view('product/add',$data);
            } else {
                redirect('/product/');
            }
            
            
        }
    }
    public function copy($id) {
        $data = $this->data;
        $result = $this->product_model->get_by_id($id);
        $data['productInfo'] = $result;
        // categories
        $data['categoryGroups'] = $this->categoryGroups;
        $data['categories'] = $this->categories;
        $data['categoriesArray'] = $this->categoriesArray;
        $data['current_pcat_code'] = $result->pcat_code;
        // brands
        $data['brands'] = $this->brands;
        $data['brandsArray'] = $this->brandsArray;
        $data['current_brandcode'] = $result->brandcode;
        
        
        $this->load->view('product/update',$data);
        
        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $dataUpdate['p_name']     = $this->input->post('pName');
            $dataUpdate['p_code']     = $this->input->post('pCode');
            $dataUpdate['p_date']     = $this->input->post('pDate');
            $dataUpdate['pcat_code']  = $this->input->post('pCatCode');
            $dataUpdate['brandcode']  = $this->input->post('pBrand');
            $dataUpdate['p_des']      = $this->input->post('pDes');
            $pictures                 = $this->input->post('pPictures'); 
            $dataUpdate['p_pictures'] = str_replace(" ","",$pictures); // remove blank(s)
            $dataUpdate['color']      = $this->input->post('pColor');
            $dataUpdate['color_img']  = $this->input->post('pColorImg');
            $dataUpdate['material']   = $this->input->post('pMaterial');
            $size                     = $this->input->post('pSize');
            $dataUpdate['size']       = implode(",",$size);
            $price                    = $this->input->post('pPrice');
            $dataUpdate['price']      = str_replace(" ","",$price); // change back to normal number format
            $dataUpdate['status']     = $this->input->post('pStatus');
            $dataUpdate['is_hot']     = $this->input->post('pIsHot');
            $dataUpdate['is_home']    = $this->input->post('pIsHome');
            $dataUpdate['is_sale']    = $this->input->post('pIsSale');
            $dataUpdate['sale_type']  = $this->input->post('pSaleType');
            $dataUpdate['sale_value'] = $this->input->post('pSaleValue');
            $dataUpdate['ordering']   = $this->input->post('pOrdering');
            
            $action = $this->input->post('action');
            $this->product_model->add($dataUpdate);
            if($action == 'btnSave') {
                $this->copy($id);
            } else {
                redirect('/product/');
            }
            
            
        }
    }
}

/* End of file product.php */
/* Location: ./application/controllers/product.php */